package com.iflysse.controller;


import com.alibaba.fastjson.JSONObject;
import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.req.KnowledgePointBoReq;
import com.iflysse.pojo.bo.resp.QuestionBoResp;
import com.iflysse.pojo.bo.resp.QuestionPlainBoResp;
import com.iflysse.pojo.bo.resp.QuestionPlainListBoResp;
import com.iflysse.pojo.po.req.QuestionIdBoReq;
import com.iflysse.pojo.vo.req.KnowledgePointVoReq;
import com.iflysse.pojo.vo.req.QuestionIdVoReq;
import com.iflysse.pojo.vo.resp.QuestionInfoResp;
import com.iflysse.pojo.vo.resp.QuestionInfoVoResp;
import com.iflysse.pojo.vo.resp.QuestionResp;
import com.iflysse.pojo.vo.resp.QuestionVoResp;
import com.iflysse.services.QuestionService;
import com.iflysse.util.CommonUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/question")
@Api(value = "老师端题库接口")
@Log4j
public class QuestionController {


    @Autowired
   private QuestionService questionService;


    /**
     * 查询问题答案详情
     */
    @PostMapping("/queryQuestionAnswer")
    @ApiOperation("查询问题答案详情")
    public QuestionInfoVoResp queryQuestionAnswer(QuestionIdVoReq questionIdVoReq){
        QuestionInfoVoResp questionInfoVoResp=new QuestionInfoVoResp();
        try{
            log.info("查询问题答案详情参数bean:"+questionIdVoReq);
            QuestionIdBoReq questionIdBoReq= CommonUtil.objToObj(questionIdVoReq,QuestionIdBoReq.class);
            QuestionBoResp questionBoResp= questionService.getQuestionByQuestionId(questionIdBoReq);
            QuestionInfoResp result=CommonUtil.objToObj(questionBoResp,QuestionInfoResp.class);
            JSONObject js=(JSONObject) JSONObject.parse(result.getQuestionContent());
            Object question=js.get("questions");
            if (question!=null&&!"".equals(question)){
                js.remove("questions");
                js.put("questionItemObjects",question);
            }

            String questionContent= JSONObject.toJSONString(js);
            result.setQuestionContent(questionContent);

            questionInfoVoResp.setResult(result);
            questionInfoVoResp.setCode(ResultEnum.SUCCESS.getCode());
            questionInfoVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("查询问题答案详情信息响应bean:"+questionInfoVoResp);

            return questionInfoVoResp;
        }catch (Exception e){
            log.error("查询问题答案详情信息出现异常："+e);
            CommonUtil.printError(questionInfoVoResp);
            return questionInfoVoResp;
        }
    }





    /**
     * 教室端 根据第三级知识点 加载题库问题
     */
    @PostMapping("/loadQuestionByKnowLedgePoint")
    @ApiOperation("教室端根据第三级知识点加载题库问题")
    public QuestionVoResp doLoadQuestionByKnowLedgePoint(KnowledgePointVoReq knowledgePointVoReq){

        QuestionVoResp questionVoResp=new QuestionVoResp();

        try{

           log.info("教室端根据第三级知识点加载题库问题参数bean:"+knowledgePointVoReq);

            KnowledgePointBoReq knowledgePointBoReq=CommonUtil.objToObj(knowledgePointVoReq,KnowledgePointBoReq.class);
            knowledgePointBoReq.setKnowledgePointId(knowledgePointVoReq.getKnowledgeParentId());
            log.info("教室端根据第三级知识点加载题库问题参数："+knowledgePointBoReq);
            QuestionPlainListBoResp questionPlainListBoResp= questionService.loadQuestionByL2KnowledgePoint(knowledgePointBoReq);
            List<QuestionPlainBoResp> loadQuestionByL2KnowledgePoint=questionPlainListBoResp.getLoadQuestionByL2KnowledgePoint();
            List<QuestionResp>result=new ArrayList<QuestionResp>();
            loadQuestionByL2KnowledgePoint.forEach(questionPlainBoResp -> {
                QuestionResp questionResp=CommonUtil.objToObj(questionPlainBoResp,QuestionResp.class);
                result.add(questionResp);
            });
            questionVoResp.setResult(result);
            questionVoResp.setCode(ResultEnum.SUCCESS.getCode());
            questionVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("教室端根据第三级知识点"+knowledgePointVoReq+"加载题库问题响应bean:"+questionVoResp);
            return questionVoResp;
        }catch (Exception e){
            log.error("教室端根据第三级知识点加载题库问题出现异常："+e);
            CommonUtil.printError(questionVoResp);
            return questionVoResp;
        }
    }


    /**
     * 查询问题详情
     */
    @PostMapping("/queryQuestionInfo")
    @ApiOperation("查询问题详情")
    public QuestionInfoVoResp queryQuestionInfo(QuestionIdVoReq questionIdVoReq) {

        QuestionInfoVoResp questionInfoVoResp=new QuestionInfoVoResp();

        try {
            log.info("查询问题信息参数bean:"+questionIdVoReq);
            QuestionIdBoReq questionIdBoReq=CommonUtil.objToObj(questionIdVoReq,QuestionIdBoReq.class);
            QuestionBoResp questionBoResp= questionService.getQuestionByQuestionId(questionIdBoReq);
            QuestionInfoResp result=CommonUtil.objToObj(questionBoResp,QuestionInfoResp.class);


            JSONObject js=(JSONObject) JSONObject.parse(result.getQuestionContent());
            Object question=js.get("questions");
            if (question!=null&&!"".equals(question)){
                js.remove("questions");
                js.put("questionItemObjects",question);
            }

            js.remove("analyze");
            js.remove("correct");
            String questionContent= JSONObject.toJSONString(js);
            result.setQuestionContent(questionContent);

            questionInfoVoResp.setResult(result);
            questionInfoVoResp.setCode(ResultEnum.SUCCESS.getCode());
            questionInfoVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("查询问题信息响应bean:"+questionInfoVoResp);

            return questionInfoVoResp;
        }catch (Exception e){
            log.error("查询问题信息出现异常："+e);
            CommonUtil.printError(questionInfoVoResp);
            return questionInfoVoResp;
        }
    }




}
